package com.example.server1.controller;

import com.example.server1.config.LogEnable;
import com.example.server1.entity.SysUser;
import com.example.server1.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author zhangW
 * @create 2024-12-10-9:20
 */
@RestController
@RequestMapping("/sys/server2")
public class SysUserController {
    @Autowired(required = false)
    private SysUserService userService;

    @Autowired(required = false)
    private RestTemplate restTemplate;

    @LogEnable
    @GetMapping("/add")
    public void insertUser() {
        System.out.println("server1------------------");
        SysUser user = new SysUser();
        user.setUserName("admin");
        user.setPassWord("123456");
        userService.save(user);
    }

    @GetMapping("/call-server2-add")
    public String callServer2Add() {
        // 调用 server2 的 /add 接口，负载均衡会在多个 server2 实例中选择
        return restTemplate.getForObject("http://server2/sys/server2/add", String.class);
    }
}
